package cool.xinya.ess.util;

import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static final String DEFAULT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATETIME_NULL = "yyyyMMddHHmmss";
    public static final String DEFAULT_DATETIME_S = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DEFAULT_DATE = "yyyy-MM-dd";
    public static final String DEFAULT_MONTH = "yyyy-MM";
    public static final String DEFAULT_DATETIME_ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZZ";


    public static Calendar getCalendar() {
        return Calendar.getInstance();
    }

    /**
     * 获取当前时间
     */
    public static Date getCurrentTime() {
        return getCalendar().getTime();
    }


    /**
     * 当前日期加多少天
     *
     * @author wangxiaolong
     * @date 2019/11/20 16:27
     */
    public static Date forwardDay(Integer dayCount) {
        Calendar calendar = getCalendar();
        calendar.add(Calendar.DATE, dayCount);
        return calendar.getTime();
    }

    public static Date forward(Integer dateType, Integer count) {
        Calendar calendar = getCalendar();
        calendar.add(dateType, count);
        return calendar.getTime();
    }

    /**
     * 时间差
     * type 1天,2小时,3分钟,4秒
     *
     * @author wangxiaolong
     * @date 2019/11/20 16:27
     */
    public static Integer timeDifference(Date startTime, Date endTime, int type) {
        Calendar startCalendar = getCalendar();
        startCalendar.setTime(startTime);
        Calendar endCalendar = getCalendar();
        endCalendar.setTime(endTime);
        long diffNum = endTime.getTime() - startTime.getTime();
        long result = 0;
        if (type == 1) {
            result = diffNum / (1000 * 60 * 60 * 24);
        } else if (type == 2) {
            result = diffNum / (1000 * 60 * 60);
        } else if (type == 3) {
            result = diffNum / (1000 * 60);
        } else if (type == 4) {
            result = diffNum / 1000;
        }
        return (int) result;
    }
}
